package com.zyg.jas.studentport.service.impl;

import com.github.pagehelper.PageHelper;
import com.zyg.jas.common.pojo.Score;
import com.zyg.jas.common.pojo.Student;
import com.zyg.jas.managerport.dao.ScoreDao;
import com.zyg.jas.managerport.service.ScoreService;
import com.zyg.jas.studentport.dao.HomeworkDao2;
import com.zyg.jas.studentport.dao.ScoreDao2;
import com.zyg.jas.studentport.service.ScoreService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

@Service
public class ScoreServiceImpl2 implements ScoreService2 {
    @Value("${jobPath}")
    private String jobPath;
    @Value("${jobURL}")
    private String jobURL;
    @Autowired
    private HomeworkDao2 homeworkDao;

    @Autowired
    private ScoreDao2 scoreDao;


    @Override
    public Map<String,Integer> getScForLevelByHid(Integer hId) {
        List<Score>  scoreList=scoreDao.selectScForLevelByHid(hId);
        int a=0;
        int b=0;
        int c=0;
        int d=0;
        for(int i=0;i<scoreList.size();i++){
            Score s = scoreList.get(i);
            if (s.getScore()<60){
                d++;
            }else if(s.getScore()>=60&&s.getScore()<80){
                c++;
            }else if (s.getScore()>=80&&s.getScore()<90){
                b++;
            }else if (s.getScore()>=90&&s.getScore()<=100){
                a++;
            }else{
                continue;
            }
        }
        Map<String,Integer> levelMap= new HashMap<>();
        levelMap.put("level1",a);
        levelMap.put("level2",b);
        levelMap.put("level3",c);
        levelMap.put("level4",d);
        return levelMap;
    }

    @Override
    public Integer getCount(String cmtId,String courseId) {
        return scoreDao.selectCount(cmtId,courseId);
    }

    @Override
    public List<Score> getScoresByCmtId(String cmtId,String courseId,Integer pageNo,Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        return scoreDao.selectScoresByCmtId(cmtId,courseId);
    }

    @Override
    public List<Score> getScreForSearch(String cmtId, String hName, String commit,String courseId,Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        return  scoreDao.selectForSearch(cmtId,hName,commit,courseId);
    }

    @Override
    public Integer getCountForSearch(Map<String, String> paramMap) {
        return scoreDao.selectCountForSearch(paramMap);
    }

    @Override
    public Integer updateScoreById(Integer id, double score, String comment) {
        return scoreDao.updateScoreById(id,score,comment);
    }

    @Override
    public List<Student> getAllStudentForSubmitJob(Integer jobId) {
        return scoreDao.selectAllStudentForSubmitJob(jobId);
    }

    @Override
    public List<Student> getStudentForSubmitJob(Integer jobId) {
        return scoreDao.selectStudentForSubmitJob(jobId);
    }

    @Override
    public List<Student> getStudentForNoSubmitJob(Integer jobId) {
        return scoreDao.selectStudentForNoSubmitJob(jobId);
    }





    @Override
    public Integer saveHomework(MultipartFile multipartFile, Integer hId,String sNo,String comId) throws IOException, ParseException {
        if (multipartFile.isEmpty()) {
            return 0;
        }
        String fileName = multipartFile.getOriginalFilename();
        int i = fileName.lastIndexOf(".");
        String suffix = fileName.substring(i);
        String fileUUID =  UUID.randomUUID().toString()+suffix;
        File dest = new File(new File(jobPath).getAbsolutePath()+ "/" + fileUUID);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        multipartFile.transferTo(dest);


        // 保存数据
        Score score = new Score();
        System.out.println("filename+   "+fileName);
        score.setCommitName(fileName);
        score.setsNo(sNo);
        score.sethId(hId);
        score.setCommitedJobUrl(jobURL+fileUUID);
        score.setCommitedJobPath(dest.getAbsoluteFile().toString());
        score.setComId(comId);
        score.setCorrectDate(new Date());
        return this.scoreDao.insertScoreOfMine(score);
    }

    @Override
    public Integer saveHomeworkForApp(MultipartFile multipartFile,String fileName, Integer hId, String sNo, String comId) throws IOException, ParseException {
        if (multipartFile.isEmpty()) {
            return 0;
        }
//        String fileName = multipartFile.getOriginalFilename();
        int i = fileName.lastIndexOf(".");
//        int i = fileName.lastIndexOf(".");
        String suffix = fileName.substring(i);
        String fileUUID =  UUID.randomUUID().toString()+suffix;
        File dest = new File(new File(jobPath).getAbsolutePath()+ "/" + fileUUID);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        multipartFile.transferTo(dest);


        // 保存数据
        Score score = new Score();
        System.out.println("filename+   "+fileName);
        score.setCommitName(fileName);
        score.setsNo(sNo);
        score.sethId(hId);
        score.setCommitedJobUrl(jobURL+fileUUID);
        score.setCommitedJobPath(dest.getAbsoluteFile().toString());
        score.setComId(comId);
        score.setCorrectDate(new Date());
        return this.scoreDao.insertScoreOfMine(score);
    }

    @Override
    public List<Score> selectMyScore(Integer hId, String sNo, String comId) {
        return this.scoreDao.selectMyScore(hId,sNo,comId);
    }

    @Override
    public Integer deleteById(Integer id) {
        return this.scoreDao.deleteById(id);
    }

}
